package my.example.samplegridview;

import my.example.samplegridview.modal.product_option;
import my.example.samplegridview.modal.user;
import o2s.utils.imageHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class login extends Activity {
	
	login _self;
	boolean _islogin;
	public static int activitycode = 1200;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		_self = this;
		_islogin = false;
		
		final ViewFlipper loginviewflipper = (ViewFlipper) findViewById(R.id.loginviewflipper);
		Bitmap companylogo = imageHelper.getImageFromResource(this, "company/sportscafe_logo.gif");
		companylogo = imageHelper.getRoundedCornerBitmap(companylogo, 4);
		ImageView iv = (ImageView)findViewById(R.id.logincompanylogo);
		iv.setImageBitmap(companylogo);
		
		Spinner tablenospinner = (Spinner) findViewById(R.id.logintableno);
		String[] stringitems = {"Table 1","Table 2","Table 3","Table 4"};
		tablenospinner.setAdapter(new tableNumberAdapter(this,stringitems));
		
		Button tosecondpage = (Button) findViewById(R.id.logintosecondpage);
		tosecondpage.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				loginviewflipper.showNext();
			}
			
		});
		
		TextView tofirstpage = (TextView) findViewById(R.id.loginbacktofirststep);
		tofirstpage.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				loginviewflipper.showPrevious();
			}
			
		});
		
		Button loginasmember = (Button) findViewById(R.id.loginloginbuttonasmember);
		loginasmember.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(_self,loginprompt.class);
				_self.startActivityForResult(i, activitycode);
			}
			
		});
		
		Button loginasguest = (Button) findViewById(R.id.loginloginbuttonasguest);
		loginasguest.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(_self,menu.class);
				i.putExtra("logintype", "guest");

				user u = currentuserdetail.temporaryUserLookup("guest", "");
				currentuserdetail.currentuser = u;
				
				_self.startActivity(i);
				_self.finish();
			}
			
		});
		
		Button signupguest = (Button) findViewById(R.id.loginsignup);
		signupguest.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent i = new Intent(_self,register.class);
				_self.startActivity(i);
			}
			
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		
		if(keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK){
			exitConfirmation();
		}
		
		return true;
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == loginprompt.activitycode){
        	if(data.getBooleanExtra("isLoginSuccess", true))
        		finish();
        }
    }
	
	public void exitConfirmation(){
		AlertDialog.Builder ab = new AlertDialog.Builder(this);
		ab.setTitle("Exit Confirmation");
		ab.setPositiveButton("Ok",new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				finish();
			}
			
		});
		ab.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
			
		});
		ab.create().show();
	}
	
	public class tableNumberAdapter extends BaseAdapter{

		String[] tablenos;
		product_option po;
		Context context;
		tableNumberAdapter _self;
		
		public tableNumberAdapter(Context context , String[] tablenos){
			this.context = context;
			this.tablenos = tablenos;
			_self = this;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return tablenos.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return tablenos[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int location, View arg1, ViewGroup arg2) {        	
			TextView spinnerlayout = null;
			if (arg1 == null) {  // if it's not recycled, initialize some attributes
            	LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            	spinnerlayout = (TextView) li.inflate(android.R.layout.simple_spinner_item, null);
            } else {
            	spinnerlayout = (TextView) arg1;
            }
			spinnerlayout.setText(tablenos[location]);
			spinnerlayout.setTextSize(30);
			return spinnerlayout;
		}
		
	}
}
